<?php
/**
 * User: zoudingyong <sheyingtg@qq.com>
 * Date: 2021/11/15
 */

namespace classes;

use OSS\OssClient;
use OSS\Core\OssException;

class Oss
{

    const endpoint = 'oss-cn-shenzhen.aliyuncs.com'; //节点
    const accessKeyId = 'LTAI4FbmzSqKQABbUzCDxeJb'; //阿里云控制台子账号key
    const accessKeySecret = 'hNKnBQSQcOVEZqRGMfEEZGOtwqGq9m'; //阿里云控制台子账号Secret
    const bucket = 'hapgolfqz'; //bucket名称


    public static function signedUrl($filepath)
    {
        //filepath为bucket下不含bucket名称为图片路径
        $bucket = self::getBucketName();
        $ossClient = self::getOssClient();
        if (is_null($ossClient)) exit(1);
        $signedUrl = $ossClient->signUrl($bucket, $filepath, 3600, 'POST');
        return $signedUrl;
    }


    private static function getOssClient()
    {
        try {
            $ossClient = new OssClient(self::accessKeyId, self::accessKeySecret, self::endpoint, false);
        } catch (OssException $e) {
            printf(__FUNCTION__ . "creating OssClient instance: FAILED\n");
            printf($e->getMessage() . "\n");
            return null;
        }
        return $ossClient;
    }

    private static function getBucketName()
    {
        return self::bucket;
    }

}